// packageC/o2o/openGroupList/openGroupList.js
let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showFilter:false,
    showDatetime:false,
    currentDate:new Date().getTime(),
    list:[],
    id:0,
    leader_name:'',
    member_num:'',
    status:'',
    statuslist: [
      {
        id: 0,
        name: '提交'
      },
      {
        id: 1,
        name: '拼团中'
      },
      {
        id: 2,
        name: '成功'
      },
      {
        id: 3,
        name: '失败'
      }
    ],

    showShare: false,
    options: [
      { name: '分享好友', icon: 'wechat', openType: 'share' }
    ],
    activeID:0,

    isLoadMore: true,
    total_page:0,
    page:1,

    activeInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      });
    }
    this.getData();
  },
  getData(tag) {
    let that = this;
    let json = {
      id: this.data.id,
      store_id: this.options.store_id,
      page: this.data.page,
      leader_name: this.data.leader_name,
      member_num: this.data.member_num,
      status: this.data.status
    };
    let urlStr = app.getNetAddresss("plugin.fight-groups.frontend.store.team-store.search");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            isLoadMore:true,
            total_page:  res.data.last_page,
            list: res.data.data
          });
          if (!tag) {
            that.getLevels();
          }else{
            that.onClickRight();
          }
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    const that = this;
    let urlStr = app.getNetAddresss('plugin.fight-groups.frontend.store.team-store.search');
    that.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: {
          id: that.data.id,
          store_id: this.options.store_id,
          page: that.data.page,
          leader_name: that.data.leader_name,
          member_num: that.data.member_num,
          status: that.data.status
        },
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              isLoadMore: true,
              list: that.data.list.concat(res.data.data)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function(res) {
          console.log(res.msg);
        }
      });
    }
  },
  getLevels(){
    let that = this;
    let urlStr = app.getNetAddresss("plugin.fight-groups.frontend.store.team-store.get-levels");
    app._getNetWork({
      url: urlStr,
      data: {
        id: this.data.id
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            levels: res.data
          });
          
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  changeEndTime(e){
    this.setData({
      changeTimeID:e.currentTarget.dataset.id,
      currentDate: e.currentTarget.dataset.endtime*1000,
      showDatetime:true
    });
  },
  toDetail(e){
    let id = e.currentTarget.dataset.id;
    if (id) {
      wx.navigateTo({
        url: '/packageC/o2o/openGroupDetail/openGroupDetail?id=' + id
      });
    }
  },
  confirmDate(){
    this.setData({
      showDatetime: false
    });
    let that = this;
    let urlStr = app.getNetAddresss("plugin.fight-groups.frontend.store.team-store.change-end-time");
    app._getNetWork({
      url: urlStr,
      data: {
        id: this.data.changeTimeID,
        end_time: this.data.currentDate/1000
      },
      success: function(resdata) {
        var res = resdata.data;
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 1000
        });
        that.getData();
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  virtualTeam(e){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '是否虚拟成团',
      success (res) {
        if (res.confirm) {
          let urlStr = app.getNetAddresss("plugin.fight-groups.frontend.store.team-store.virtual-team");
          app._getNetWork({
            url: urlStr,
            data: {
              team_id: e.currentTarget.dataset.id
            },
            success: function(resdata) {
              var res = resdata.data;
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 1000
              });
              that.getData();
            },
            fail: function(res) {
              console.log(res);
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },
  onChange(e) {
    this.setData({
      leader_name: e.detail,
    });
  },
  confirm(){
    this.getData(true);
  },
  setMember_num(e){
    let member_num = e.currentTarget.dataset.num;
    if (this.data.member_num == member_num) {
      this.setData({
        member_num:''
      });
      return;
    }
    this.setData({
      member_num:member_num
    });
    this.confirm();
  },
  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
  setStatus(e){
    let status = e.currentTarget.dataset.id;
    if (this.data.status === status) {
      this.setData({
        status:''
      });
      return;
    }
    this.setData({
      status:status
    });
    this.confirm();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  
  onClickRight(){
    let status = !this.data.showFilter;
    this.setData({
      showFilter:status
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  clickShare(e){
    this.setData({
      activeID:e.target.dataset.id,
      showShare:true,
      ['activeInfo.title']: e.target.dataset.title,
      ['activeInfo.img']: e.target.dataset.img
    });
  },
  onClose(){
    this.setData({
      showShare:false
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    var value = wx.getStorageSync('yz_uid');
    var mid = '';
    if (value) {
      mid = value;
    }
    let _params ={
      title: '门店拼团开团列表',
      path: '/packageC/o2o/openGroupList/openGroupList?mid=' + mid
    };
    if(e.from=="button"){
      _params.title=this.data.activeInfo.title,
      _params.imageUrl= this.data.activeInfo.img,
      _params.path= '/packageB/member/group/GroupDetail/GroupDetail?id=' + this.data.activeID + '&store_id='+this.options.store_id+'&mid=' + mid;
    }
    console.log("1111111111111",_params);
    this.data.goodshare_info={};
    return _params;
  }
});